package com.hsh.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.hsh.bean.*;
import com.hsh.service.CommentService;
import com.hsh.util.DateTimeFormatUtil;
import com.hsh.util.ToBootStrapUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentRestController {


    @Resource
    private CommentService commentService;

    /**
     * 用户信息分页显示
     * @param offset
     * @param limit
     * @param comment
     * @return
     */
    @RequestMapping("/list-data")
    public String list(@RequestParam(defaultValue = "0") int offset,
                       @RequestParam(defaultValue = "5") int limit,
                       Comment comment) {
         comment.setUid(null);
        //分页查询
        PageInfo<Comment> commentPageInfo = commentService.findCommentByPage(comment, offset / limit + 1, limit);
        System.out.println("comment---controller---list");
        System.out.println(comment);
        commentPageInfo.getList().forEach(System.out::println);
        String json = ToBootStrapUtil.toJSON(commentPageInfo);
        return json;
    }



    @RequestMapping("/info-data")
    public String info(@RequestParam(defaultValue = "0") int offset,
                       @RequestParam(defaultValue = "5") int limit,
                       Integer cTargetId){
        PageInfo<Comment> commentInfo = commentService.findCommentInfo(cTargetId,offset / limit + 1, limit);
        commentInfo.getList().forEach(System.out::println);
        String json = ToBootStrapUtil.toJSON(commentInfo);
        return json;
    }

    @RequestMapping("/delete-data")
    public String delete(Integer cid){
        Integer delete = commentService.delete(cid);
        Message msg = new Message();
        msg.setState(delete - 1);
        msg.setResult(delete == 1 ?"删除成功":"删除失败");
        return JSON.toJSONString(msg);
    }

    @RequestMapping("add-data")
    public String add(Comment comment){
        Integer insert = commentService.insert(comment);
        Message msg = new Message();
        msg.setState(insert - 1);
        msg.setResult(insert == 1 ?"添加成功":"添加失败");
        return JSON.toJSONString(msg);
    }


}
